package com.xcjw.com.GlideUtils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

/**
 * @packname com.homeclient.com.GlideUtils
 * @filename GlideRoundTransform
 * @date on 2018/8/7 11:32
 *****/
public class GlideRoundTransform extends BitmapTransformation {

      private static float radius = 0f;

      public GlideRoundTransform(Context context) {
            this(context, 8);
      }

      public GlideRoundTransform(Context context, int dp) {
            super(context);
            this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
      }

      @Override
      protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return roundCrop(pool, toTransform);
      }

      private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;
            Bitmap bitmap = changeBitmapSize(source);
            Bitmap result = pool.get(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            if (result == null) {

                  int width = bitmap.getWidth();
                  int height = bitmap.getHeight();
                  result = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
            }
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            RectF rectF = new RectF(0f, 0f, bitmap.getWidth(), bitmap.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint);
            return result;
      }
      public static Bitmap changeBitmapSize(Bitmap bitmap) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            //设置想要的大小
            int newWidth = 400;
            int newHeight = 400;
            //计算压缩的比率
            float scaleWidth=((float)newWidth)/width;
            float scaleHeight=((float)newHeight)/height;
            //获取想要缩放的matrix
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth,scaleHeight);
            //获取新的bitmap
            bitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
            bitmap.getWidth();
            bitmap.getHeight();
//            Log.e("newWidth","newWidth"+bitmap.getWidth());
//            Log.e("newHeight","newHeight"+bitmap.getHeight());
            return bitmap;
      }

      @Override public String getId() {
            return getClass().getName() + Math.round(radius);
      }
}